/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MyLogPanel.java
 *
 * Created on 24 Απρ 2011, 10:43:22 μμ
 */
package myComponents;

import java.awt.Point;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import myComponents.myGUI.buttons.MyAbstractButton;
import myComponents.myTableCellRenderers.MyLogTypeRenderer;
import tools.MySeriesLogger;

/**
 *
 * @author ssoldatos
 */
public class MyLogPanel extends javax.swing.JPanel {

  private static final long serialVersionUID = 2367461746217L;
  private DefaultTableModel model = new DefaultTableModel();
  public static final int COLUMN_TYPE = 0;
  public static final int COLUMN_TIME = 1;
  public static final int COLUMN_TITLE = 2;
  public static final int COLUMN_MESSAGE = 3;

  /** Creates new form MyLogPanel */
  public MyLogPanel() {
    if(MySeriesLogger.logger != null){
    MySeriesLogger.logger.log(Level.INFO, "Creating log panel");
    MySeriesLogger.logger.log(Level.INFO, "Initializing components");
    }
    initComponents();
    if(MySeriesLogger.logger != null){
    MySeriesLogger.logger.log(Level.FINE, "Components initialized");
    }
    table.getColumnModel().getColumn(COLUMN_TYPE).setMaxWidth(50); 
    table.getColumnModel().getColumn(COLUMN_TYPE).setCellRenderer(new MyLogTypeRenderer());
   
    table.getColumnModel().getColumn(COLUMN_TIME).setPreferredWidth(180);
    table.getColumnModel().getColumn(COLUMN_TIME).setMaxWidth(200);
    
    table.getColumnModel().getColumn(COLUMN_TITLE).setPreferredWidth(250);
    table.getColumnModel().getColumn(COLUMN_TITLE).setMaxWidth(400);
    
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    
    
  }

  public void log(int type, long timestamp, String title, String message) {
    ((DefaultTableModel) table.getModel()).insertRow(0, new Object[]{type, getDateTime(timestamp),title, message});
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    title = new javax.swing.JLabel();
    bt_hide = new myComponents.myGUI.buttons.MyButtonCancel();
    jScrollPane1 = new javax.swing.JScrollPane();
    table = new javax.swing.JTable();
    bt_clearMessages = new myComponents.myGUI.buttons.MyDefaultButton(MyAbstractButton.CLEAR,"Clear messages");

    title.setText("Messages Panel");

    bt_hide.setToolTipText("Hide");
    bt_hide.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bt_hideActionPerformed(evt);
      }
    });

    table.setAutoCreateRowSorter(true);
    table.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] {

      },
      new String [] {
        "Type", "Time", "Title", "Message"
      }
    ) {
      Class[] types = new Class [] {
        java.lang.Integer.class, java.lang.Long.class, java.lang.String.class, java.lang.String.class
      };
      boolean[] canEdit = new boolean [] {
        false, false, false, false
      };

      public Class getColumnClass(int columnIndex) {
        return types [columnIndex];
      }

      public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit [columnIndex];
      }
    });
    table.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseEntered(java.awt.event.MouseEvent evt) {
        tableMouseEntered(evt);
      }
    });
    table.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
      public void mouseMoved(java.awt.event.MouseEvent evt) {
        tableMouseMoved(evt);
      }
    });
    jScrollPane1.setViewportView(table);

    bt_clearMessages.setToolTipText("Clear messages");
    bt_clearMessages.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bt_clearMessagesActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 561, Short.MAX_VALUE)
            .addGap(10, 10, 10))
          .addGroup(layout.createSequentialGroup()
            .addComponent(title, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
            .addGap(135, 135, 135)
            .addComponent(bt_clearMessages, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(bt_hide, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
          .addComponent(bt_hide, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(bt_clearMessages, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(title))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
        .addContainerGap())
    );
  }// </editor-fold>//GEN-END:initComponents

    private void bt_hideActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_hideActionPerformed
      setVisible(false);
    }//GEN-LAST:event_bt_hideActionPerformed

    private void bt_clearMessagesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_clearMessagesActionPerformed
      ((DefaultTableModel) table.getModel()).setRowCount(0);
    }//GEN-LAST:event_bt_clearMessagesActionPerformed

private void tableMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableMouseEntered

}//GEN-LAST:event_tableMouseEntered

private void tableMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableMouseMoved
    Point p = evt.getPoint();
    int row = table.rowAtPoint(p);
    String title = (String) table.getModel().getValueAt(row, COLUMN_TITLE);
    String time = (String) table.getModel().getValueAt(row, COLUMN_TIME);
    String mess = (String) table.getModel().getValueAt(row, COLUMN_MESSAGE);
    table.setToolTipText(createTooltip(time, title, mess));
}//GEN-LAST:event_tableMouseMoved

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private myComponents.myGUI.buttons.MyDefaultButton bt_clearMessages;
  private myComponents.myGUI.buttons.MyButtonCancel bt_hide;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTable table;
  private javax.swing.JLabel title;
  // End of variables declaration//GEN-END:variables

  private Object getDateTime(long timestamp) {
    SimpleDateFormat sdf = new SimpleDateFormat("d/MM/y HH:mm:ss SSS");
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(timestamp);
    return sdf.format(c.getTime());
  }
  
  private String createTooltip(String time, String title , String mess){
    String html="";
    html+="<html><table>";
    html+="<tr><td><b>"+title+"</b></td></tr>";
    html+="<tr><td>(<i>"+time+"</i>)</td></tr>";
    html+="<tr><td>"+mess.replaceAll("\\.", ".<br />") +"</td></tr>";
    html+="</table></html>";
    return html;
  }
}
